<?php
include_once("config.php");
doCSS();
if (!$_POST["submit"] && $_SESSION["loggedIn"]) {
	echo"
	<html>
	<script src='cp.js' type='text/javascript'></script>
	<div id='cp' class='cp'></div>
	<body>
	<form action='car_add.php' method='POST' enctype='multipart/form-data'>
	<table>
		<tr>
		<td class= up colspan=2>Добавить авто</td>
		</tr>
		<tr>

			<td class=n>Модель авто</td>
			<td class=n><input type='text' name='car_model' value='' maxlength='30'>
			<input type=hidden name='car_color' id='test1' size='9' value=''>
			Цвет:
			<input type='text' disabled='true' id='test2' size='1' value=''>			
			<img src=images/cp.png onclick = showColorGrid3('test1','test2') alt=\"Выбрать цвет\"></img></td>
		</tr>

		<tr>
			<td class=n>Номер авто(цифры)</td>
			<td  class=n>●<input type='numeric' size='3' name='car_num' value='' maxlength='3'>●●</td>
		</tr>

		<tr>
			<td class=n>Опыт вождения</td>
			<td class=n><input type='text' size='2' name='exp_dr' value='' maxlength='2'>
			<select>
			<option name='exp_sel' value=12>год(-а)/лет</option>
			<option name='exp_sel' value=1>месяца(-ев)</option>
			</select></td>
		</tr>

		<tr>
			<td class=n>Фото</td>
			<td class=n><input type='file' name='photo'></td>
		</tr>
	<tr>
			<td class=bot colspan=2>
			<center><input name='submit' type='submit' value='Сохранить'></center>
			</td>
		</tr>
	</table>
	</form>
	</body>
	</html>
	";
}
else if ($_POST["submit"] && $_SESSION["loggedIn"]) {
	$car_model=$_POST["car_model"];
	$car_num=$_POST["car_num"];
	$car_color=$_POST["car_color"];
	$exp_dr=$_POST["exp_dr"];
	$exp_sel=$_POST["exp_sel"];
	$exp=$exp_dr*$exp_sel;
	if ($_FILES["photo"]["name"] != '')
		{
			$dir = "cars//";
			$name_of_file = $_FILES["photo"]["name"];
			$_FILES["photo"]["name"] = rand(0, 100000);
			$_FILES["photo"]["name"] .= "_".$name_of_file;
			copy($_FILES["photo"]["tmp_name"], "cars//".$_FILES["photo"]["name"]);
			$photo = $_FILES["photo"]["name"];
		}
	addCar(getUserIdByLogin($_SESSION['login']),$car_model,$car_num,$car_color,$exp,$photo);
	echo "<meta http-equiv='refresh'; content='2; url=profile.php'>";
	echo "<br>Информация об авто успешно добавлена. Перенаправление..";
}
else displayErrors("login", "", 500);

?>